<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Hotkey - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The Hotkey command creates, modifies, enables, or disables a hotkey while the script is running." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Hotkey</h1>

<p>Creates, modifies, enables, or disables a hotkey while the script is running.</p>

<pre class="Syntax">
<span class="func">Hotkey</span>, KeyName <span class="optional">, Label, Options</span>
<span class="func">Hotkey</span>, IfWinActive/Exist <span class="optional">, WinTitle, WinText</span>
<span class="func">Hotkey</span>, If <span class="optional">, Expression</span>
<span class="func">Hotkey</span>, If, % FunctionObject
</pre>
<h2>Parameters</h2>
<dl>

  <dt>KeyName</dt>
  <dd><p>Name of the hotkey's activation key, including any <a href="../Hotkeys.htm#Symbols">modifier symbols</a>. For example, specify <code>#c</code> for the <kbd>Win</kbd>+<kbd>C</kbd> hotkey.</p>
    <p>If <em>KeyName</em> already exists as a hotkey, that hotkey will be updated with the values of the command's other parameters.</p>
    <p><em>KeyName</em> can also be the name of an existing hotkey label (i.e. a double-colon label), which will cause that hotkey to be updated with the values of the command's other parameters.</p>
    <p>When specifying an <em>existing</em> hotkey, <em>KeyName</em> is not case sensitive. However, the names of keys must be spelled the same as in the existing hotkey (e.g. Esc is not the same as Escape for this purpose). Also, the order of <a href="../Hotkeys.htm#Symbols">modifier symbols</a> such as ^!+# does not matter. <a href="GetKey.htm">GetKeyName()</a> can be used to retrieve the standard spelling of a key name.</p>
    <p>When a hotkey is first created -- either by the Hotkey command or a <a href="../Hotkeys.htm">double-colon label</a> in the script -- its key name and the ordering of its modifier symbols becomes the permanent name of that hotkey as reflected by <a href="../Variables.htm#ThisHotkey">A_ThisHotkey</a>. This name is shared by all <a href="_IfWinActive.htm#variant">variants</a> of the hotkey, and does not change even if the Hotkey command later accesses the hotkey with a different symbol ordering.</p>
    <p><span class="ver">[v1.1.15+]:</span> If the hotkey variant already exists, its behavior is updated according to whether <em>KeyName</em> includes or excludes the <a href="../Hotkeys.htm#Tilde">tilde (~) prefix</a>. However, prior to <span class="ver">[v1.1.19]</span>, the hotkey was not updated if <em>Label</em> was omitted.</p>
    <p><span class="ver">[v1.1.19+]:</span> The <a href="../Hotkeys.htm#prefixdollar">use hook ($) prefix</a> can be added to existing hotkeys. This prefix affects all variants of the hotkey and cannot be removed. Prior to <span class="ver">[v1.1.19]</span>, the prefix was ignored when modifying an existing hotkey variant.</p>
    </dd>

  <dt>Label</dt>
  <dd><p>The name of the <a href="../misc/Labels.htm">label</a> whose contents will be executed (as a new <a href="../misc/Threads.htm">thread</a>) when the hotkey is pressed. Both normal labels and <a href="../Hotkeys.htm">hotkey</a>/<a href="../Hotstrings.htm">hotstring</a> labels can be used. The trailing colon(s) should not be included. If <em>Label</em> is dynamic (e.g. %VarContainingLabelName%), <a href="IsLabel.htm">IsLabel(VarContainingLabelName)</a> may be called beforehand to verify that the label exists.</p>
  <p id="Functor"><span class="ver">[v1.1.20+]:</span> If not a valid label name, this parameter can be the name of a function, or a single variable reference containing a <a href="../objects/Functor.htm">function object</a>. For example, <code>Hotkey %FuncObj%, On</code> or <code>Hotkey % FuncObj, On</code>. Other expressions which return objects are currently unsupported. When the hotkey executes, the function is called without parameters. Hotkeys can also be <a href="../Hotkeys.htm#Function">defined as functions</a> without the Hotkey command.</p>
      <p>This parameter can be left blank if <em>KeyName</em> already exists as a hotkey, in which case its label will not be changed. This is useful to change only the hotkey's <em>Options</em>.</p>
      <p class="note"><strong>Note</strong>: If the label or function is specified but the hotkey is disabled from a previous use of this command, the hotkey will remain disabled. To prevent this, include the word ON in <em>Options</em>.</p>
      <p>This parameter can also be one of the following special values:</p>
      <p><strong>On</strong>: The hotkey becomes enabled. No action is taken if the hotkey is already On.</p>
      <p><strong>Off</strong>: The hotkey becomes disabled. No action is taken if the hotkey is already Off.</p>
      <p><strong>Toggle</strong>: The hotkey is set to the opposite state (enabled or disabled).</p>
      <p><strong>AltTab</strong> (and others): These are special Alt-Tab hotkey actions that are described <a href="../Hotkeys.htm#alttab">here</a>.</p>
      <p class="warning"><strong>Caution:</strong> Defining a label named On, Off, Toggle or AltTab (or any variation recognized by this command) may cause inconsistent behavior. It is strongly recommended that these values not be used as label names.</p>
      </dd>

  <dt>Options</dt>
  <dd><p>A string of zero or more of the following letters with optional spaces in between. For example: <code>UseErrorLevel B0</code>.</p>
      <p id="UseErrorLevel"><strong>UseErrorLevel</strong>: If the command encounters a problem, this option skips the warning dialog, sets <a href="../misc/ErrorLevel.htm">ErrorLevel</a> to one of the codes from the table <a href="#ErrorLevel">below</a>, then allows the <a href="../misc/Threads.htm">current thread</a> to continue.</p>
      <p><strong>On</strong>: Enables the hotkey if it is currently disabled.</p>
      <p><strong>Off</strong>: Disables the hotkey if it is currently enabled. This is typically used to create a hotkey in an initially-disabled state.</p>
      <p><strong>B</strong> or <strong>B0</strong>: Specify the letter B to buffer the hotkey as described in <a href="_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a>. Specify B0 (B with the number 0) to disable this type of buffering.</p>
      <p><strong>Pn</strong>: Specify the letter P followed by the hotkey's <a href="../misc/Threads.htm">thread priority</a>. If the P option is omitted when creating a hotkey, 0 will be used.</p>
      <p><strong>Tn</strong>: Specify the letter T followed by a the number of threads to allow for this hotkey as described in <a href="_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>. For example: <code>T5</code>.</p>
      <p><strong>In</strong> (InputLevel) <span class="ver">[v1.1.23+]</span>: Specify the letter I (or i) followed by the hotkey's <a href="_InputLevel.htm">input level</a>. For example: <code>I1</code>.</p>
      <p>If any of the option letters are omitted and the hotkey already exists, those options will not be changed. But if the hotkey does not yet exist -- that is, it is about to be created by this command -- the options will default to those most recently in effect. For example, the instance of <a href="_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a> that occurs closest to the bottom of the script will be used. If <a href="_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a> does not appear in the script, its default setting (OFF in this case) will be used. This behavior also applies to <a href="_IfWinActive.htm">#IfWin</a>: the bottommost occurrence applies to newly created hotkeys unless &quot;<a href="#IfWin">Hotkey IfWin</a>&quot; has executed since the script started.</p>
      </dd>

  <dt>IfWinActive<br>
    IfWinExist<br>
    IfWinNotActive<br>
    IfWinNotExist<br>
    If, Expression<br>
    If, % FunctionObject</dt>
  <dd><p>These sub-commands make all subsequently-created hotkeys context sensitive. See <a href="#IfWin">below</a> for details.</p></dd>

  <dt>WinTitle<br>
    WinText</dt>
  <dd><p>Within these parameters, any variable reference such as %var% becomes permanent the moment the command finishes. In other words, subsequent changes to the contents of the variable are not seen by existing IfWin hotkeys.</p>
      <p>Like <a href="_IfWinActive.htm">#IfWinActive/Exist</a>, <em>WinTitle</em> and <em>WinText</em> use the default settings for <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a> and <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> as set in the <a href="../Scripts.htm#auto">auto-execute section</a>. See <a href="_IfWinActive.htm">#IfWinActive/Exist</a> for details.</p></dd>

  <dt id="If">If, Expression</dt>
  <dd>
    <p><span class="ver">[AHK_L 4+]:</span> Associates subsequently-created hotkeys with a given #If expression. <em>Expression</em> must be an expression which has been used with the <a href="_If.htm">#If directive</a> elsewhere in the script. Although this command is unable to create new expressions, it can create new hotkeys using an existing expression. See <a href="_If.htm#ExDynamic">#If example 4</a>.</p>
    <p class="note"><strong>Note:</strong> The Hotkey command uses the string that you pass to it, not the original source code. Commas and deref chars (percent signs) are interpreted <em>before</em> the command is called, so may need to be escaped if they are part of the original expression.  <a href="_EscapeChar.htm">Escape sequences</a> are resolved when the script loads, so only the resulting characters are considered; for example, <code>Hotkey, If, x = "`t"</code> and <code>Hotkey, If, % "x = """ A_Tab """"</code> both correspond to <code>#If x = "`t"</code>.</p>
    <p><strong>Known limitation:</strong> If <em>Expression</em> contains an <code>and</code>/<code>or</code> operator, it is not recognized as an existing expression. As a workaround, use the equivalent <code>&amp;&amp;</code>/<code>||</code> operator in both the original #If expression and the one passed to the Hotkey command.</p>
  </dd>
  
  <dt id="IfFn">If, % FunctionObject</dt>
  <dd>
    <p><span class="ver">[v1.1.25+]:</span> Associates subsequently-created hotkeys with a given <a href="../objects/Functor.htm">function object</a>. Such hotkeys will only execute if calling the given function object yields a non-zero number. This is like <code>Hotkey, If, Expression</code>, except that each hotkey can have many <a href="#variant">variants</a> (one per object). <em>FunctionObject</em> must be a single variable (not an expression) containing an object with a <em>call</em> method. The function or <em>call</em> method can accept one parameter, the <a href="../Variables.htm#ThisHotkey">name</a> of the hotkey.</p>
    <p>Once passed to the Hotkey command, the object will never be deleted (but memory will be reclaimed by the OS when the process exits).</p>
    <p>The <a href="#ExampleIfFn">"three-key combination" example</a> below uses this sub-command.</p>
  </dd>

</dl>

<h2 id="ErrorLevel">Error Handling</h2>
<p><span class="ver">[v1.1.04+]</span>: This command is able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is changed only when: 1) the first parameter is IfWin[Not]Active/Exist, in which case it is set to 1 if there was a problem or 0 otherwise; or 2) the word UseErrorLevel is present in the <em>Options</em> parameter.</p>
<p><span class="ver">[v1.1.25+]:</span> If the first parameter is "If", an exception is thrown if the second parameter is invalid or a memory allocation fails. ErrorLevel is not set in those cases, but is still set to 0 on success.</p>
<table class="info">
  <tr>
    <th>Error</th>
    <th>Description</th>
  </tr>
  <tr>
    <td>1</td>
    <td>The <em>Label</em> parameter specifies a nonexistent label name.</td>
  </tr>
  <tr>
    <td>2</td>
    <td>The <em>KeyName</em> parameter specifies one or more keys that are either not recognized or not supported by the current keyboard layout/language.</td>
  </tr>
  <tr>
    <td>3</td>
    <td>Unsupported prefix key. For example, using the mouse wheel as a prefix in a hotkey such as <code>WheelDown &amp; Enter</code> is not supported.</td>
  </tr>
  <tr>
    <td>4</td>
    <td>The <em>KeyName</em> parameter is not suitable for use with the <a href="../Hotkeys.htm#alttab">AltTab or ShiftAltTab</a> actions. A combination of two keys is required. For example: <code>RControl &amp; RShift::AltTab</code>.</td>
  </tr>
  <tr>
    <td>5</td>
    <td>The command attempted to modify a nonexistent hotkey.</td>
  </tr>
  <tr>
    <td>6</td>
    <td>The command attempted to modify a nonexistent <a href="#variant">variant</a> of an existing hotkey. To solve this, use <a href="#IfWin">Hotkey IfWin</a> to set the criteria to match those of the hotkey to be modified.</td>
  </tr>
  <tr>
    <td>98</td>
    <td>Creating this hotkey would exceed the limit of hotkeys per script (however, each hotkey can have an unlimited number of <a href="#variant">variants</a>, and there is no limit to the number of <a href="../Hotstrings.htm">hotstrings</a>). The limit was raised from 700 to 1000 in <span class="ver">[v1.0.48]</span>, and to 32762 in <span class="ver">[v1.1.30]</span>.</td>
  </tr>
  <tr>
    <td>99</td>
    <td>Out of memory. This is very rare and usually happens only when the operating system has become unstable.</td>
  </tr>
</table>
<p><br>
Tip: The UseErrorLevel option can be used to test for the existence of a hotkey variant. For example:</p>
<pre>Hotkey, ^!p,, UseErrorLevel
if ErrorLevel in 5,6
    MsgBox The hotkey does not exist or it has no variant for the current IfWin criteria.</pre>

<h2>Remarks</h2>
<p>The <a href="#IfWin">current IfWin setting</a> determines the <a href="#variant">variant</a> of a hotkey upon which the Hotkey command will operate.</p>
<p>If the goal is to disable selected hotkeys or hotstrings automatically based on the type of window that is active, <code>Hotkey, ^!c, Off</code> is usually less convenient than using <a href="_IfWinActive.htm">#IfWinActive/Exist</a> (or their dynamic counterparts &quot;Hotkey IfWinActive/Exist&quot; <a href="#IfWin">below</a>).</p>
<p>Creating hotkeys via <a href="../Hotkeys.htm">double-colon labels</a> performs better than using the Hotkey command because the hotkeys can all be enabled as a batch when the script starts (rather than one by one). Therefore, it is best to use this command to create only those hotkeys whose key names are not known until after the script has started running. One such case is when a script's hotkeys for various actions are configurable via an <a href="IniRead.htm">INI file</a>.</p>
<p>A given label can be the target of more than one hotkey. If it is known that a label was called by a hotkey, you can determine which hotkey by checking the built-in variable <a href="../Variables.htm#ThisHotkey">A_ThisHotkey</a>.</p>
<p>If the script is <a href="Suspend.htm">suspended</a>, newly added/enabled hotkeys will also be suspended until the suspension is turned off (unless they are exempt as described in the <a href="Suspend.htm">Suspend</a> section).</p>
<p>The <a href="_InstallKeybdHook.htm">keyboard</a> and/or <a href="_InstallMouseHook.htm">mouse</a> hooks will be installed or removed if justified by the changes made by this command.</p>
<p>Although the Hotkey command cannot directly enable or disable hotkeys in scripts other than its own, in most cases it can <a href="../misc/Override.htm">override</a> them by creating or enabling the same hotkeys. Whether this works depends on a combination of factors: 1) Whether the hotkey to be overridden is a <a href="ListHotkeys.htm">hook hotkey</a> in the other script (non-hook hotkeys can always be overridden); 2) The fact that the most recently started script's hotkeys generally take precedence over those in other scripts (therefore, if the script intending to override was started most recently, its override should always succeed);  3) Whether the enabling or creating of this hotkey will newly activate the <a href="_InstallKeybdHook.htm">keyboard</a> or <a href="_InstallMouseHook.htm">mouse</a> hook (if so, the override will always succeed).</p>
<p>Once a script has at least one hotkey, it becomes persistent, meaning that <a href="ExitApp.htm">ExitApp</a> rather than Exit should be used to terminate it. Hotkey scripts are also automatically <a href="_SingleInstance.htm">#SingleInstance</a> unless <code>#SingleInstance Off</code> has been specified.</p>

<h2 id="IfWin">Remarks About <em>Hotkey, If</em></h2>
<p>The &quot;Hotkey If&quot; commands allow context-sensitive <a href="../Hotkeys.htm">hotkeys</a> to be created and modified while the script is running (by contrast, the <a href="_If.htm">#If</a> and <a href="_IfWinActive.htm">#IfWinActive/Exist</a> directives are positional and take effect before the script begins executing). For example:</p>
<pre>Hotkey, IfWinActive, ahk_class Notepad
Hotkey, ^!e, MyLabel  <em>; Creates a hotkey that works only in Notepad.</em></pre>
<p>Using &quot;Hotkey If&quot; puts context sensitivity into effect for all subsequently created or modified <a href="../Hotkeys.htm">hotkeys</a>. In addition, each If sub-command is mutually exclusive; that is, only the most recent one will be in effect.</p>
<p>To turn off context sensitivity (that is, to make subsequently-created hotkeys work in all windows), specify any If sub-command but omit the parameters. For example: <code>Hotkey, If</code> or <code>Hotkey, IfWinActive</code>.</p>
<p>If &quot;Hotkey If&quot; is never used by a script, the bottommost use of any <a href="_If.htm">#If</a> or <a href="_IfWinActive.htm">#IfWin</a> directive (if any) will be in effect for the Hotkey command.</p>
<p>When a mouse or keyboard hotkey is disabled via an If sub-command or directive, it performs its native function; that is, it passes through to the active window as though there is no such hotkey. However, joystick hotkeys always pass through, whether they are disabled or not.</p>

<h2 id="variant">Variant (Duplicate) Hotkeys</h2>
<p>A particular hotkey can be created more than once if each definition has different IfWin criteria. These are known as <em>hotkey variants</em>. For example:</p>
<pre>Hotkey, IfWinActive, ahk_class Notepad
Hotkey, ^!c, MyLabelForNotepad
Hotkey, IfWinActive, ahk_class WordPadClass
Hotkey, ^!c, MyLabelForWordPad
Hotkey, IfWinActive
Hotkey, ^!c, MyLabelForAllOtherWindows</pre>
<p>If more than one variant of a hotkey is eligible to fire, only the one created earliest will fire. The exception to this is the global variant (the one with no IfWin criteria): It always has the lowest precedence, and thus will fire only if no other variant is eligible.</p>
<p>When creating duplicate hotkeys, the order of <a href="../Hotkeys.htm#Symbols">modifier symbols</a> such as ^!+# does not matter. For example, <code>^!c</code> is the same as <code>!^c</code>. However, keys must be spelled consistently. For example, <em>Esc</em> is not the same as <em>Escape</em> for this purpose (though the case does not matter). Finally, any hotkey with a <a href="../Hotkeys.htm#wildcard">wildcard prefix (*)</a> is entirely separate from a non-wildcard one; for example, <code>*F1</code> and <code>F1</code> would each have their own set of variants.</p>
<p>For more information about IfWin hotkeys, see <a href="_IfWinActive.htm#gen">#IfWin's General Remarks</a>.</p>
<h2>Related</h2>
<p><a href="../Hotkeys.htm#Symbols">Hotkey Symbols</a>, <a href="_IfWinActive.htm">#IfWinActive/Exist</a>, <a href="_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a>, <a href="_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a>, <a href="Suspend.htm">Suspend</a>, <a href="IsLabel.htm">IsLabel()</a>, <a href="../misc/Threads.htm">Threads</a>, <a href="Thread.htm">Thread</a>, <a href="Critical.htm">Critical</a>, <a href="Gosub.htm">Gosub</a>, <a href="Return.htm">Return</a>, <a href="Menu.htm">Menu</a>, <a href="SetTimer.htm">SetTimer</a></p>
<h2>Examples</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a></p>
<pre>Hotkey, ^!z, MyLabel
return

MyLabel:
MsgBox You pressed %A_ThisHotkey%.
return</pre>
</div>

<div class="ex" id="ExOther">
<p><a href="#ExOther">#2</a>: Other examples</p>
<pre>Hotkey, RCtrl &amp; RShift, AltTab <em>; Makes RCtrl &amp; RShift operate like Alt-Tab.</em>
Hotkey, #c, On  <em>; Re-enables the Win-C hotkey.</em>
Hotkey, $+#c, Off  <em>; Disables the Shift-Win-C hotkey.</em>
Hotkey, ^!a, , T5  <em>; Changes the hotkey to allow 5 threads.</em>

Hotkey, IfWinActive, ahk_class Notepad
Hotkey, ^!c, MyLabelForNotepad  <em>; Creates Ctrl-Alt-C as a hotkey that works only in Notepad.</em></pre>
</div>

<div class="ex" id="ExampleIfFn">
<p><a href="#ExampleIfFn">#3</a>: This GUI allows you to register primitive three-key combination hotkeys:</p>
<pre>Gui Add, Text, xm, Prefix key:
Gui Add, Edit, yp x100 w100 vPrefix, Space
Gui Add, Text, xm, Suffix hotkey:
Gui Add, Edit, yp x100 w100  vSuffix, f &amp; j
Gui Add, Button, Default, Register
Gui Show
return

ButtonRegister() {
    global
    Gui Submit, NoHide
    local fn
    fn := Func("HotkeyShouldFire").Bind(Prefix)
    Hotkey If, % fn
    Hotkey % Suffix, FireHotkey
}

HotkeyShouldFire(prefix, thisHotkey) {
    return GetKeyState(prefix)
}

FireHotkey() {
    MsgBox %A_ThisHotkey%
}

GuiClose:
GuiEscape:
ExitApp</pre>
</div>

</body>
</html>
